// wx.cloud.init({
// 	env:"house-1g1ct5l5ad438a2d"
// })
const db = wx.cloud.database({
	throwOnNotFound: false,
})
const _ = db.command;
const $ = db.command.aggregate
/**
 * showdoc
 * @catalog 云开发·公共功能·获取数据
 * @title 获取数据
 * @description 获取数据
 * @param table 必选 string  表名
 * @param where 必选 object  条件
 * @param page  必选 number  页码
 * @param limit 必选 number  单页显示
 * @url cloud/getData 
 */
const getData = async (table, param = {
	page: 1,
	limit: 10
},lookUp) => {
	try {
		if (typeof param.page == 'undefined' || param.limit <= 20) {
			let Db = db.collection(table)
			if (typeof param.where != "undefined") {
				Db = Db.where(param.where)
			}
			if (typeof param.orderBy != "undefined") {
				Db = Db.orderBy(param.orderBy[0], param.orderBy[1])
			}
			if (typeof param.page != 'undefined') {
				Db = Db.skip((parseInt(param.page) - 1) * parseInt(param.limit)).limit(parseInt(param.limit))
			}
			let res = await Db.get();
			return res.errMsg == 'collection.get:ok' ? res.data : [];
		} else {
			param.$url = 'getData'
			param.table = table
			if (typeof lookUp != 'undefined') {
				param.lookUp = lookUp
			}
			let res = await wx.cloud.callFunction({
				name: 'models',
				data: param
			})
			return res.result
		}
	} catch (e) {
		console.log(e)
		return []
	}
}

/**
 * showdoc
 * @catalog 云开发·公共功能·添加数据
 * @title 添加数据
 * @description 添加数据
 * @param table 必选 string  表名
 * @param data  必选 object  数据
 * @url cloud/addData 
 */
const addData = async (table, data) => {
	try {
		let res = await db.collection(table).add({
			data
		});
		return res.errMsg == 'collection.add:ok' ? res._id : false;
	} catch (e) {
		return false
	}
}
/**
 * showdoc
 * @catalog 云开发·公共功能·编辑数据
 * @title 编辑数据
 * @description 编辑数据
 * @param table 必选 string  表名
 * @param where 必选 object  条件
 * @param data  必选 object  数据
 * @url cloud/editData 
 */
const editData = async (table, where = {}, data) => {
	if (JSON.stringify(where) == '{}') return false
	try {
		let res = await db.collection(table).where(where).update({
			data
		});
		return res.errMsg == 'collection.update:ok' && res.stats.updated > 0 ? true : false;
	} catch (e) {
		console.log(e)
		return false
	}
}

const setData = async (table, id, data) => {
	if (!id) return false
	try {
		let res = await db.collection(table).doc(id).update({
			data
		});
		return res.errMsg == 'document.update:ok' && res.stats.updated > 0 ? true : false;
	} catch (e) {
		console.log(e)
		return false
	}
}
/**
 * showdoc
 * @catalog 云开发·公共功能·删除数据
 * @title 删除数据
 * @description 删除数据
 * @param table 必选 string  表名
 * @param where 必选 object  条件
 * @url cloud/delData 
 */
const delData = async (table, where = {}) => {
	if (JSON.stringify(where) == '{}') return false
	try {
		let res = await db.collection(table).where(where).remove();
		return res.errMsg == 'collection.remove:ok' ? true : false;
	} catch (e) {
		console.log(e)
		return false
	}
}
/**
 * showdoc
 * @catalog 云开发·公共功能·自增
 * @title 自增
 * @description 自增
 * @param table 必选 string  表名
 * @param where 必选 object  条件
 * @param field 必选 string  操作字段
 * @param num   必选 number  操作数
 * @url cloud/incNum 
 */
const incNum = async (table, where = {}, field = '', num = 0) => {
	if (JSON.stringify(where) == '{}' || !num || !table || !field) return false
	try {
		let data = {}
		data[field] = _.inc(num);
		if (num < 0) {
			where[field] = _.gte(Math.abs(num));
		}
		let res = await db.collection(table).where(where).update({
			data
		});
		return res.errMsg == 'collection.update:ok' && res.stats.updated ? true : false;
	} catch (e) {
		console.log(e)
		return false
	}
}

/**
 * showdoc
 * @catalog 云开发·公共功能·获取单条数据
 * @title 获取单条数据
 * @description 获取单条数据
 * @param table 必选 string  表名
 * @param where 必选 object  条件
 * @param page  必选 number  页码
 * @param limit 必选 number  单页显示
 * @url cloud/getOne 
 */
const getOne = async (table, param = {}) => {
	try {
		let res = await db.collection(table).where(param).get();
		return res.errMsg == 'collection.get:ok' ? res.data[0] : false;
	} catch (e) {
		console.log(e)
		return false
	}
}
/**
 * showdoc
 * @catalog 云开发·公共功能·获取二维码
 * @title 获取经纬度页面二维码
 * @description 获取二维码
 * @param name 必选 string  函数名
 * @param data 必选 object  数据
 * @param $url 必选 string  方法名
 * @url cloud/getMiniCode 
 */
const callFunction = async (name = '', data = {
	$url: ''
}) => {
	if (!name || !data.$url) return false;
	try {
		let res = await wx.cloud.callFunction({
			name: name,
			data
		})
		// console.log(res)
		return res.result
	} catch (e) {
		console.log(e)
		return false
	}
}
/**
 * showdoc
 * @catalog 云开发·公共功能·获取数量
 * @title 获取数量
 * @description 获取数量
 * @param table 必选 string  表名
 * @param where 必选 object  条件
 * @url cloud/getCount 
 */
const getCount = async (table, where = {}) => {
	if (!table) return 0;
	try {
		let res = await db.collection(table).where(where).count();
		return res.errMsg == 'collection.count:ok' ? res.total : 0;
	} catch (e) {
		console.log(e)
		return 0
	}
}
/**
 * showdoc
 * @catalog 云开发·公共功能·上传文件
 * @title 上传文件
 * @description 上传文件
 * @param fileName 必选 string  文件名
 * @param filePath 必选 string  文件路径
 * @url cloud/uploadFile 
 */
const uploadFile = async (fileName, filePath) => {
	try {
		if (!fileName || !filePath) return false;
		let res = await wx.cloud.uploadFile({
			cloudPath: fileName,
			filePath: filePath
		})
		if (res.errMsg == 'cloud.uploadFile:ok') return res.fileID;
		return false;
	} catch (e) {
		console.log(e)
		return false;
	}
}
/**
 * showdoc
 * @catalog 云开发·公共功能·删除文件
 * @title 删除文件
 * @description 删除文件
 * @param fileIdList 必选 array  文件ID列表
 * @url cloud/delFile 
 */
const delFile = async (fileIdList) => {
	try {
		let res = await wx.cloud.deleteFile({
			fileList: fileIdList
		})
		return res.fileList
	} catch (e) {
		console.log(e)
		return []
	}
}
/**
 * showdoc
 * @catalog 云开发·公共功能·获取openid
 * @title 获取openid
 * @description 获取openid
 * @url cloud/getOpenid
 */
const getOpenid = async () => {
	try {
		let res = await wx.cloud.callFunction({
			name: "common",
			data: {
				$url: "getOpenid",
			}
		})
		if (res.result.errCode == 200) {
			return res.result.openid
		}
		return false
	} catch (e) {
		console.log(e)
		return false
	}
}

const getPhone = async (e) => {
	if (!e) return false;
	try {
		let res = await wx.cloud.callFunction({
			name: "login",
			data: {
				$url: "getPhone",
				cloudID: e.detail.cloudID
			}
		})
		if (res.result.errCode == 200) {
			return res.result.phone
		}
		return false
	} catch (e) {
		console.log(e)
		return false
	}
}

module.exports = {
	addData,
	getData,
	getOne,
	editData,
	setData,
	delData,
	getCount,
	uploadFile,
	delFile,
	getOpenid,
	getPhone,
	callFunction,
	incNum,
	_,
	db,
	$,
}
